package com.xiaomengkuan.product.controller;

import com.xiaomengkuan.common.entity.vo.PageVo;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.product.entity.pojo.Product;
import com.xiaomengkuan.product.entity.pojo.Story;
import com.xiaomengkuan.product.entity.vo.AdminTagGroupVo;
import com.xiaomengkuan.product.entity.vo.ProductResponseVo;
import com.xiaomengkuan.product.service.ProductService;
import com.xiaomengkuan.product.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-02-19 3:02 下午, File Encoding: utf8 --
 */
@Api(tags = "商品后台管理")
@RestController
@RequestMapping(value = "product/admin")
public class AdminProductController {
    @Resource
    ProductService productService;
    @Resource
    TagService tagService;

    @PostMapping("getAll_Admin")
    @ApiOperation(value = "获取所有商品")
    public R getAll_Admin(@RequestBody Map<String, Object> requestBody) {
        PageVo<ProductResponseVo> page = productService.getAll_Admin(requestBody);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).page(page);
    }

    @GetMapping("tags")
    @ApiOperation(value = "查询所有标签（分组)")
    public R allTags() {
        List<AdminTagGroupVo> tagGroups = tagService.allTags();
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putList(tagGroups);
    }

    @PostMapping("insert")
    @ApiOperation(value = "添加一个商品")
    public R insert(@RequestBody Map<String, Object> requestBody) {
        return productService.insert(requestBody);
    }

    @PostMapping("stories/{productId}")
    @ApiOperation(value = "获取商品的全部故事")
    public R stories(@PathVariable("productId") String productId,
                     @RequestBody Map<String, Object> requestBody) {
        List<Story> stories = productService.getStories(productId, requestBody);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).putList(stories);
    }

    @GetMapping("story/{id}")
    @ApiOperation(value = "获取一个故事")
    public R getStory(@PathVariable("id") String id) {
        return productService.getStory(id);
    }

    @PostMapping("saveStory")
    @ApiOperation(value = "保存修改故事")
    public R saveStory(@RequestBody Map<String, Object> requestBody) {
        return productService.saveStory(requestBody);
    }

    @PostMapping("createStory")
    @ApiOperation(value = "新建一个故事")
    public R createStory(@RequestBody Map<String, Object> requestBody) {
        return productService.createStory(requestBody);
    }

    @DeleteMapping("deleteStory/{id}")
    @ApiOperation(value = "删除故事")
    public R deleteStory(@PathVariable("id") String id) {
        return productService.deleteStory(id);
    }

    @GetMapping("pictures/{id}")
    @ApiOperation(value = "获取所有图片")
    public R pictures(@PathVariable("id") String id) {
        return productService.pictures(id);
    }

    @PostMapping("savePicture")
    @ApiOperation(value = "保存一张图片")
    public R pictures(@RequestBody Map<String, Object> requestBody) {
        return productService.savePicture(requestBody);
    }

    @DeleteMapping("deletePicture/{id}")
    @ApiOperation(value = "删除一张图片")
    public R deletePicture(@PathVariable("id") String id) {
        return productService.deletePicture(id);
    }

    @GetMapping("info/{id}")
    @ApiOperation(value = "获取商品的基本信息")
    public R info(@PathVariable("id") String id) {
        return productService.getInfo(id);
    }

    @PostMapping("saveInfo")
    @ApiOperation(value = "保存商品基本信息")
    public R saveInfo(@RequestBody Map<String, Object> requestBody) {
        return productService.saveInfo(requestBody);
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "删除商品")
    public R removeProduct(@PathVariable("id") String id) {
        return productService.removeProduct(id);
    }

}
